package com.example.securitydemo.configure;

import com.example.securitydemo.authentication.JwtSecurityConfigurer;
import com.example.securitydemo.authentication.MyUserDetailsService;
import com.example.securitydemo.properties.SecurityProperties;
import com.example.securitydemo.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)// 开启使用注解进行权限验证
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private SecurityProperties securityProperties;
    @Autowired
    private MyUserDetailsService myUserDetailsService;
    @Autowired
    private TokenUtils tokenUtils;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * 密码加密工具
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        //springSecurity升级到5.x后,推荐BCryptPasswordEncoder方法加密
        return new BCryptPasswordEncoder();
    }

    /**
     * 配置用户权限获取和密码加密
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //取消csrf防护
                .csrf()
                .disable()
                //允许页面frame嵌套
                .headers()
                .frameOptions()
                .disable()
                //开启cors支持
                .and()
                .cors()
                //关闭session
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                //设置免认证 url
                .and()
                .authorizeRequests()
                .antMatchers(securityProperties.getAnonUrl())
                .permitAll()
                //其他url需要认证
                .anyRequest()
                .authenticated()
                //添加JWT过滤器
                .and()
                .apply(new JwtSecurityConfigurer(tokenUtils));
    }

    /**
     * 配置静态资源访问不需要认证
     *
     * @param web
     */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring()
                .antMatchers(HttpMethod.OPTIONS, "/**")
                .antMatchers("/test.txt");
    }
}
